package ModelLayer;
import java.util.ArrayList;

//singleton
public class ExampleContainer
{
    private static ExampleContainer exampleContainer;
    private ArrayList<Example> examples;
    
    /**
     * Singleton constructor
     */
    private ExampleContainer()
    {
        examples = new ArrayList<Example>();
    }
    
    /**
     * Returns the static instance of this object (ensuring only one exists at a time)
     */
    public static ExampleContainer getInstance()
    {
        if(exampleContainer == null)
        {
            exampleContainer = new ExampleContainer();
        }
        
        return exampleContainer;      
    }
    
    /**
     * Creates an example, associates this example with a DVD, and puts in the ArrayList examples
     */
    public void addExample(int serialNumber, String dateOfPurchase, double priceOfPurchase, Dvd dvd)
    {   
        Example example = new Example(serialNumber, dateOfPurchase, priceOfPurchase, dvd);
        examples.add(example);
    }
    
    /**
     * Removes the example with serialNumber as serial number from the list of examples.
     */
    public void removeExample(int serialNumber)
    {
        //test to see if the example exists
       Example exampleTest = searchExample(serialNumber);   //<-- local (i.e. private) method searchExample()
       if(exampleTest != null)
       {
          examples.remove(exampleTest);
       }
    }
    
    /**
     * Returns all the DvdExamples
     */
    public ArrayList<Example> getAllExamples()
    {
        return examples;
    }
    
    /**
     * Returns the example with a given serialNumber if it does exist.
     * Else returns null
     */
    private Example searchExample(int serialNumber)
    {
        //search pattern
        int index = 0;
        boolean found = false;
        while(found == false && index < examples.size())
        {
            Example example = examples.get(index);
            if(example.getSerialNumber() == serialNumber)
            {
                found = true;
            }
            else
            {
                index = index + 1;
            }
        }
        
        if(found == true)
        {
            return examples.get(index);
        }
        
        return null;
    }
}